/**
 * Created by mylxsw on 14-6-14.
 */
$(function(){
    // 限制页面中所有图片的宽度
    $("button.limit-img").click(function(){
        chrome.tabs.executeScript({
           code: "$('img').addClass('aicode-ext-width-500')"
        });
    });
    $("button.limit-img-restore").click(function(){
        chrome.tabs.executeScript({
            code: "$('img').removeClass('aicode-ext-width-500')"
        });
    });
    // 隐藏页面中所有图片
    $("button.remove-img").click(function(){
       chrome.tabs.executeScript({
          code: "$('img').addClass('aicode-ext-hide')"
       });
    });
    $("button.remove-img-restore").click(function(){
        chrome.tabs.executeScript({
            code: "$('img').removeClass('aicode-ext-hide')"
        });
    });

    // 登陆
    if(storage.getValue('token', '') == ''){
        $("#main-area").html($('#login').html());
    }else{
        $("#main-area").html($('#bookmarks').html());
    }


    // 注册账号
    //chrome.bookmarks.MAX_WRITE_OPERATIONS_PER_HOUR = 10000;
    //chrome.bookmarks.MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE = 10000;



    $("#main-area").delegate(".btn-login","click", function(){
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();

        $.post("http://aicode.cc/api/login", {username: username, password: password}, function(data){
           if(data.status == '1'){
               storage.setValue('token', data.data.token);

               $('.message').addClass('alert').addClass('alert-success').html('成功登陆!');
               $("#main-area").html($('#bookmarks').html());
           } else{
               $('.message').addClass('alert').addClass('alert-danger').html(data.info);
           }
        }, "json");

    }).delegate(".bookmark-bakup", "click", function(){
        chrome.bookmarks.getTree(function(result){
            var token = storage.getValue('token', '');

            $.post("http://aicode.cc/api/chrome", {action: 'backup', bookmarks: JSON.stringify(result), token: token}, function(data){
                if(data.status == '1'){
                    $('.message').addClass('alert').addClass('alert-success').html('成功同步到云端!');
                }else{
                    $('.message').addClass('alert').addClass('alert-danger').html(data.info);
                }
            }, "json");
        });
    }).delegate(".btn-logout", "click", function(){
        var token = storage.getValue('token', '');
        $.post("http://aicode.cc/api/logout", {token: token}, function(data){
            if(data.status == '1'){
                storage.remove('token');
                $('.message').addClass('alert').addClass('alert-success').html('成功退出!');
                $("#main-area").html($('#login').html());
            } else{
                $('.message').addClass('alert').addClass('alert-danger').html(data.info);
            }
        }, "json");
    }).delegate('.bookmark-clear', 'click', function(){
            for(var x = 1; x <= 3; x ++){
                chrome.bookmarks.getSubTree(x + '', function(result){
                    for(var i = 0; i < result[0].children.length; i ++){
                        var id = result[0].children[i].id;
                        chrome.bookmarks.removeTree(id, function(){
                            console.log(id);
                        });
                    }
                });
            }
    }).delegate('.bookmark-sync', 'click', function(){
            var token = storage.getValue('token', '');
            $.get('http://aicode.cc/api/chrome', {action: 'sync', token: token}, function(data){
                if(data.status == 1){
                    if(data.data.length > 0){
//                        var bookmarks = data.data;
//                        var cur = 0;
//
//                        var createBookmarkObj = function(bookmark){
//                          return  {
//                              parentId: bookmark['parent_id'] + '',
//                              index: bookmark['bindex'],
//                              title: bookmark['title'],
//                              url: bookmark['url']
//                          }
//                        };
//                        chrome.bookmarks.create(createBookmarkObj(bookmarks[6]), function(res){
//                            console.log(res);
//                        });

                        chrome.bookmarks.create({
                            parentId: '1',
                            index: 1,
                            title: '哈哈',
                            url: 'http://www.baidu.com'
                        });
//                        var createBookmark = function(result){
//                            result = result || {};
//                            //console.log(result);
//                            if(cur < bookmarks.length){
//                                cur ++;
//                                if(bookmarks[cur]['bid'] <= 3){
//                                    createBookmark();
//                                    return false;
//                                }
//                                var obj = createBookmarkObj(bookmarks[cur]);
////                                window.setTimeout(function(){
//
//                                chrome.bookmarks.create(obj, createBookmark)
////                                }, 0);
//                            }
//                        };
//
//
//                        createBookmark();
                    }
                    $('.message').addClass('alert').addClass('alert-success').html('同步成功!');
                }else{
                    $('.message').addClass('alert').addClass('alert-danger').html(data.info);
                }
            },'json');
    });



});